package org.axed.user.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;

/**
 * 
 */
public class UndoBuffer {
	public static final int DELTEXT     = 1;
	public static final int JOINLINE    = 2;
	public static final int REMOVECHARS = 3;
	public static final int SPLITLINE   = 4;
	public static final int TEXT        = 5; //TODO
	public static final int INSTEXT     = 6;

	public int type;
	public AxedPointer p1;
	public AxedPointer p2;
	public StringBuffer text;

	public int atomicID;
	public String desc;

	/**
	 * Constructor
	 */
	public UndoBuffer(AxedArea axed) {
		text = new StringBuffer();
		p1 = new AxedPointer(axed);
		p2 = new AxedPointer(axed);
	}

	/**
	 * Appends a char
	 */
	public void append(char ch) {
		assert isTextBuffer();
		text.append(ch);
	}

	/**
	 * Return text
	 */
	public String getText() {
		assert isTextBuffer();
		return text.toString();
	}



	/**
	 * Inserts a char 
	 */
	public void insert(char ch, int p) {
		assert isTextBuffer();
		text.insert(p, ch);
	}

	/**
	 * Returns true if this buffer type holds a text.
	 */
	public boolean isTextBuffer() {
		switch (type) {
		case DELTEXT :
		case INSTEXT :
		case REMOVECHARS :
		case TEXT :
			return true;
		default :
			return false;
		}
	}

	/**
	 * This is a backspace removed text.
	 */
	public void setRemoveChars(AxedPointer p) {
		type = REMOVECHARS;
		this.p1.set(p);
		this.text.delete(0, this.text.length());
	}

	/**
	 * This is a cut removed text.
	 */
	public void setDelText(AxedInterval iv, String text) {
		type = DELTEXT;
		this.p1.set(iv.p1);
		this.p2.set(iv.p2);
		this.text.delete(0, this.text.length());
		this.text.append(text);
	}

	/**
	 * This is a paste inserted text.
	 */
	public void setInsText(AxedInterval iv, String text) {
		type = INSTEXT;
		this.p1.set(iv.p1);
		this.p2.set(iv.p2);
		this.text.delete(0, this.text.length());
		this.text.append(text);
	}


	/**
	 * This is an inserted char text.
	 */
	public void setText(AxedPointer p) {
		type = TEXT;
		this.p1.set(p);
		this.text.delete(0, this.text.length());
	}

	/**
	 * This is a join-removed line
	 */
	public void setJoinLine(AxedPointer p) {
		type = JOINLINE;
		this.p1.set(p);
		this.text.delete(0, this.text.length());
	}


	/**
	 * This is a new line.
	 */
	public void setSplitline(AxedPointer p) {
		type = SPLITLINE;
		this.p1.set(p);
		this.text.delete(0, this.text.length());
	}

}
